// Lex file: sq.l
alpha [A-Za-z]
digit [0-9]
%%

[ \t\n]
select        return SELECT;
distinct     return DISTINCT;
from         return FROM;
where       return WHERE;
like           return LIKE;
desc         return DESC;
asc           return ASC;
"group by"    return GROUP;
having           return HAVING;
"order by"     return ORDER;
or                  return OR;
and                return AND;
in                   return IN;
{digit}+        return NUM;
{alpha}({alpha}|{digit})* return ID;
"<="              return LE;
">="              return GE;
"=="              return EQ;
"!="               return NE;
.                     return yytext[0];
%%

// Yacc file: sq.y

%{
#include <stdio.h>
#include <stdlib.h>

%}
%token ID NUM SELECT DISTINCT FROM WHERE LE GE EQ NE OR AND LIKE GROUP HAVING ORDER ASC DESC IN
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE

%%

    S    : ST1';' {printf("INPUT ACCEPTED...\n");exit(0);};
    ST1    : SELECT attributeList FROM tableList ST2
        | SELECT DISTINCT attributeList FROM tableList ST2
        ;
    ST2    : WHERE COND ST3
        | ST3
        ;
    ST3    : GROUP attributeList ST4
        | ST4
        ;
    ST4    : HAVING COND ST5
        | ST5
        ;
    ST5    : ORDER attributeList ST6
        |
        ;
    ST6    : DESC
        | ASC
        |
        ;
  attributeList :     ID','attributeList
        | '*'
        | ID
        ;
 tableList    : ID',' tableList
        | ID
        ;
    COND    : COND OR COND
        | COND AND COND
        | E
        | ID IN '(' ST1 ')'
        ;
    E    : F'=' F
        | F '<' F
        | F '>' F 
        | F LE F
        | F GE F
        | F EQ F
        | F NE F
        | F OR F
        | F AND F
        | F LIKE F
        ;
    F    : ID
        | NUM 
        ;
%%
#include"lex.yy.c"
#include<ctype.h>
main()
{
    printf("Enter the query:");
    yyparse();
}       

Output:

nn@linuxmint ~ $ lex sq.l
nn@linuxmint ~ $ yacc sq.y
nn@linuxmint ~ $ gcc y.tab.c -ll -ly
nn@linuxmint ~ $ ./a.out
Enter the query:select model from product where manufacture in ( select manuid from manufactures where manufacture = keltron);
INPUT ACCEPTED...
nn@linuxmint ~ $ 
